package com.homework.demo4;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class Exam {
    HashSet<Student> students;

    public void gradeAndPrintRanking() {
        // 计算每个学生的成绩
        for (Student student : students) {
            int score = 0;
            for (Map.Entry<String, String> entry : student.answerSheet.answers.entrySet()) {
                String questionNumber = entry.getKey();
                String studentAnswer = entry.getValue();
                String correctAnswer = student.paper.questions.get(questionNumber).correctAnswer;
                if (studentAnswer.equals(correctAnswer)) {
                    score++;
                }
            }
            student.score = score;
        }

        // 对学生按成绩降序排序
        List<Student> sortedStudents = new ArrayList<>(students);
        sortedStudents.sort((s1, s2) -> s2.score - s1.score);

        // 打印成绩排名
        for (int i = 0; i < sortedStudents.size(); i++) {
            System.out.println((i + 1) + ". " + sortedStudents.get(i).name + ": " + sortedStudents.get(i).score);
        }
    }
}
